<!--
 * @Descripttion: 表单列表
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 14:23:05
-->
<template>
    <div>
        <top-nav :title="navTitle" :isBack="true"></top-nav>
        <div class="page-main">
          <el-table
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
            <el-table-column
              prop="id"
              label="ID">
            </el-table-column>
            <el-table-column
              prop="name"
              label="姓名"
              width="180">
            </el-table-column>
            <el-table-column
              prop="phone"
              label="手机号">
            </el-table-column>
            <el-table-column
              prop="content"
              label="内容"
              width="300">
            </el-table-column>
            <el-table-column
              prop="content"
              label="上传图片"
              width="400">
              <template slot-scope="scope">
                <div class="c-title flex-warp" v-if="scope.row.imgs && scope.row.imgs.length > 0">
                  <lb-cover :fileList='scope.row.imgs' :isToDel="false" size="small" fileType='image' type='more' :fileSize="scope.row.imgs && scope.row.imgs.length"></lb-cover>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="phone"
              label="状态">
              <template slot-scope="scope">
                  <el-tag size="mini" :type="scope.row.status === 1 ? 'success' : 'info' ">{{scope.row.status === 1 ? '未处理' : '已处理'}}</el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop='create_time'
              label="创建时间"
              width="120">
              <template slot-scope="scope">
                <div>{{scope.row.create_time | handleTime(1)}}</div>
                <div>{{scope.row.create_time | handleTime(2)}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="phone"
              label="操作">
              <template slot-scope="scope">
                <div class="table-operate">
                    <lb-button v-if="scope.row.status === 1" size='mini' plain type='primary' @click="confirmOperateForm(scope.row.id)">{{$t('action.handle')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="confirmDelForm(scope.row.id)">{{$t('action.delete')}}</lb-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <lb-page
            :batch='false'
            :page='searchForm.page'
            :pageSize='searchForm.limit'
            :total='total'
            @handleSizeChange='handleSizeChange'
            @handleCurrentChange='handleCurrentChange'
            >
            </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      loading: false,
      tableData: [],
      searchForm: {
        limit: 10,
        page: 1,
        id: ''
      },
      total: 0
    }
  },
  created () {
    let {id, title} = this.$route.query
    this.searchForm.id = id
    this.navTitle = title
  },
  activated () {
    this.getTableDataList()
  },
  components: {
  },
  methods: {
    getTableDataList () {
      let {searchForm} = this
      this.$api.getWebsiteContentList(searchForm).then(res => {
        console.log(res)
        if (res.code === 200) {
          let {data, total} = res.data
          data.map(item => {
            item.imgs = item.imgs ? item.imgs.map(aitem => {
              return {url: aitem}
            }) : ''
          })
          this.tableData = data
          this.total = total
        }
      })
    },
    handleSwitchChange (method, wid) {
      let {id} = this.searchForm
      this.$api.updateWebsiteContentInfo({id: wid, method, modular_id: id}).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(method === 2 ? this.$t('tips.successDel') : this.$t('tips.successRev'))
          if (method === 2) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (method === 2) return
          this.getTableDataList()
        }
      })
    },
    confirmOperateForm (id) {
      this.$confirm(this.$t('tips.confirmOperate'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.handleSwitchChange(0, id)
      })
    },
    confirmDelForm (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.handleSwitchChange(2, id)
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
